package com.yssoft.service.special;


import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yssoft.entity.special.TSMediaExposureEntity;
import com.yssoft.entity.special.TsPersonnelEntity;
import com.yssoft.mapper.special.TSMediaExposureMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class TSMediaExposureService extends ServiceImpl<TSMediaExposureMapper, TSMediaExposureEntity> {



    @Resource
    private TSMediaExposureMapper mediaExposureMapper;

   /* public boolean addMediaExposure(TSMediaExposureEntity mediaExposure) {
        if (mediaExposure == null || mediaExposure.getEnterpriseId() == null
                || mediaExposure.getExposureName() == null) {
            return false;
        }

        // 设置默认值
        if (mediaExposure.getDelFlag() == null) {
            mediaExposure.setDelFlag(0);
        }
        if (mediaExposure.getCreateTime() == null) {
            mediaExposure.setCreateTime(new Date());
        }
        if (mediaExposure.getUpdateTime() == null) {
            mediaExposure.setUpdateTime(new Date());
        }
        if(mediaExposure.getUpdateBy() == null){
            mediaExposure.setUpdateBy("");
        }

        return mediaExposureMapper.insertMed(mediaExposure) > 0;
    }

    public boolean updateMediaExposure(TSMediaExposureEntity mediaExposure) {
        if (mediaExposure == null || mediaExposure.getId() == null) {
            return false;
        }
        return mediaExposureMapper.updateMedById(mediaExposure) > 0;
    }

    *//**
     * 软删除
     *//*
    @Transactional(rollbackFor = Exception.class)
    public boolean delete(TSMediaExposureEntity entity) {
        LambdaUpdateWrapper<TSMediaExposureEntity> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(TSMediaExposureEntity::getId, entity.getId()) // 根据 id 匹配
                .set(TSMediaExposureEntity::getDelFlag, "1"); // 设置 deleteId 的值
        return this.update(wrapper);
    }

    public boolean deleteById(Long id, String updateBy) {
        if (id == null || updateBy == null || updateBy.trim().isEmpty()) {
            return false;
        }
        return mediaExposureMapper.deleteById(id, updateBy) > 0;
    }

    public TSMediaExposureEntity getById(Long id) {
        if (id == null) {
            return null;
        }
        return mediaExposureMapper.selectById(id);
    }*/

    public List<TSMediaExposureEntity> getAllByPage(int pageNum, int pageSize, Long companyId) {
        // 校验分页参数
        if (pageNum < 1) {
            pageNum = 1;
        }
        if (pageSize < 1 || pageSize > 100) {
            pageSize = 10;
        }
        int offset = (pageNum - 1) * pageSize;
        return mediaExposureMapper.getAll(companyId);
    }

   /* public List<TSMediaExposureEntity> getAllByPageWithEnterpriseId(int pageNum, int pageSize, Long enterpriseId) {
        // 校验分页参数
        if (pageNum < 1) {
            pageNum = 1;
        }
        if (pageSize < 1 || pageSize > 100) {
            pageSize = 10;
        }
        int offset = (pageNum - 1) * pageSize;
        return mediaExposureMapper.selectAll(enterpriseId);
    }

    public List<TSMediaExposureEntity> getByEnterpriseId(Long enterpriseId) {
        if (enterpriseId == null) {
            return null;
        }
        return mediaExposureMapper.selectByEnterpriseId(enterpriseId);
    }

    *//*public List<TSMediaExposureEntity> searchByExposureName(String exposureName) {
        if (exposureName == null || exposureName.trim().isEmpty()) {
            return getAllByPage(1, getTotalCount());
        }
        return mediaExposureMapper.selectByExposureName(exposureName.trim());
    }*//*

    public int getTotalCount() {
        return mediaExposureMapper.selectTotalCount();
    }

    public int getTotalCount(Long enterpriseId) {
        return mediaExposureMapper.selectTotalCountByCompanyId(enterpriseId);
    }*/
    /**
     * 保存
     */
    @Transactional(rollbackFor = Exception.class)
    public boolean addTSMediaExposure(TSMediaExposureEntity entity){
        entity.setId(UUID.randomUUID().toString().replaceAll("-", ""));

/*
		tsThreeYearsSales.setCreateBy(loginUser.getId().toString());
*/
        if(this.save(entity)){
            return true;
        }
        return false;
    }

    /**
     * 修改根居ID
     */
    @Transactional(rollbackFor = Exception.class)
    public boolean update(TSMediaExposureEntity entity) {
        if(this.updateById(entity)){
            return true;
        }
        return false;
    }

    /**
     * 软删除
     */
    @Transactional(rollbackFor = Exception.class)
    public boolean delete(TSMediaExposureEntity entity) {
        entity.setDelFlag(1);
        return this.updateById(entity);
    }

}
